package com.bdqn.controller;

import com.bdqn.pojo.StorageRecord;
import com.bdqn.pojo.Supplier;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.StorageRecordService;
import com.bdqn.service.SupplierService;
import com.bdqn.util.PageSupport;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("storageRecord")
public class StorageRecordController {
    @Resource
    private StorageRecordService storageRecordService;
    @Resource
    private SupplierService supplierService;

    @GetMapping("/list")
    public String list(Model model, @RequestParam(value = "queryGoodsName", required = false) String queryGoodsName, @RequestParam(value = "querySupplierId", required = false) Long querySupplierId, @Param("queryPayStatus")Long queryPayStatus , @RequestParam(value = "pageIndex", defaultValue = "1") Long pageIndex) {
        List<StorageRecord> storageRecords = null;
        List<Supplier> suppliers = null;
        try {
            //总数量
            long totalCount = storageRecordService.count(queryGoodsName, querySupplierId);
            //总页数
            PageSupport page = new PageSupport();
            page.setPageSize(page.getPageSize());       // 设置一页显示数据量
            page.setTotalPageCount(totalCount);   // 设置总数据量
            page.setCurrentPageNo(pageIndex);
            page.setTotalCount(totalCount);
            // 设置当前页数

            long totalPageCount = page.getTotalPageCount();

            if (pageIndex > totalPageCount) {
                pageIndex = totalPageCount;
                throw new RuntimeException("页码不正确");
            }
            storageRecords = storageRecordService.page(queryGoodsName, querySupplierId,queryPayStatus,page);
            suppliers = supplierService.list();
            page.setData(storageRecords);
            model.addAttribute("storageRecordList", storageRecords);  // 用户信息l
            model.addAttribute("supplierList", suppliers);
            model.addAttribute("page", page); // 查询条件回显
            model.addAttribute("queryGoodsName", queryGoodsName); // 查询条件回显
            model.addAttribute("querySupplierId", querySupplierId);
            model.addAttribute("querypayStatus", queryPayStatus);

        } catch (Exception e) {
            e.printStackTrace();
            return "redirect://error";
        }
        return "storageRecord/list";
    }
}
